/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FeeManagementf.java
 *
 * Created on 22-08-2011, 21:27:29
 */
package tuitionmanagement.gui;

import Business.FeeLevelBuss;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import tuitionmanagement.DAO.FeeLevelDao;
import tuitionmanagement.entity.Fee;

/**
 *
 * @author XUAN
 */
public class FeeManagement extends javax.swing.JPanel {

   Fee fee = new Fee();
    FeeLevelBuss feebuss = new FeeLevelBuss();
    FeeLevelDao feedao = new FeeLevelDao();
    ArrayList<Fee> lst;
    int index;
    ArrayList<Fee> listFee = new ArrayList<Fee>();
    Vector vrowFee = new Vector();
    public int confirm;
    Vector vRow = new Vector();

    /** Creates new form Fee */
    public FeeManagement() {
        initComponents();
        lst = feebuss.readByAll();
        loadTableFeeDetail(index);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtFeeLevelId = new javax.swing.JTextField();
        txtPaidTime = new javax.swing.JTextField();
        txtTotal = new javax.swing.JTextField();
        dcBegin = new com.toedter.calendar.JDateChooser();
        dcEnd = new com.toedter.calendar.JDateChooser();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbFeeLevel = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        btnAddFee = new javax.swing.JButton();
        btnEditFee = new javax.swing.JButton();
        btnDeleteFee = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        jPanel1.setLayout(new java.awt.GridBagLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setForeground(new java.awt.Color(0, 0, 255));
        jLabel1.setText("FeeLevel Management");
        jPanel1.add(jLabel1, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jPanel1, gridBagConstraints);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel6.setText("Fee LevelId:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(jLabel6, gridBagConstraints);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Paid Time:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(jLabel2, gridBagConstraints);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Total Money:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(jLabel3, gridBagConstraints);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setText("Date Begin:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(jLabel4, gridBagConstraints);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel5.setText("Date End");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(jLabel5, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.ipadx = 150;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(txtFeeLevelId, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 150;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(txtPaidTime, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.ipadx = 150;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(txtTotal, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipadx = 130;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(dcBegin, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.ipadx = 130;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(dcEnd, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jPanel2, gridBagConstraints);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "FeeLevel Details", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12), new java.awt.Color(0, 0, 255))); // NOI18N
        jPanel3.setLayout(new java.awt.GridBagLayout());

        tbFeeLevel.setFont(new java.awt.Font("Tahoma", 0, 12));
        tbFeeLevel.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "FeeLevelId", "PaidTime", "TotalMoney", "DateBegin", "DateEnd"
            }
        ));
        tbFeeLevel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbFeeLevelMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbFeeLevel);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.ipadx = 400;
        gridBagConstraints.ipady = 100;
        jPanel3.add(jScrollPane1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jPanel3, gridBagConstraints);

        jPanel4.setLayout(new java.awt.GridBagLayout());

        btnAddFee.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnAddFee.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/add2.png"))); // NOI18N
        btnAddFee.setText("Add");
        btnAddFee.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddFeeActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel4.add(btnAddFee, gridBagConstraints);

        btnEditFee.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnEditFee.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/edit.png"))); // NOI18N
        btnEditFee.setText("Edit");
        btnEditFee.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditFeeActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel4.add(btnEditFee, gridBagConstraints);

        btnDeleteFee.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnDeleteFee.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/delete2.png"))); // NOI18N
        btnDeleteFee.setText("Delete");
        btnDeleteFee.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteFeeActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel4.add(btnDeleteFee, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        add(jPanel4, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    public void loadTableFeeLevel(ArrayList<Fee> lst) {

        for (int i = 0; i < lst.size(); i++) {
            Vector v = new Vector();
            fee = lst.get(i);
            v.addElement(fee.getFeeLevelId());
            v.addElement(fee.getPaidTime());
            v.addElement(fee.getMoneyTotal());
            v.addElement(fee.getDateStart());
            v.addElement(fee.getDateEnd());

            vRow.addElement(v);
        }

        Vector vCol = new Vector();
        vCol.addElement("FeeLevelID");
        vCol.addElement("PaidTime");
        vCol.addElement("MoneyTotal");
        vCol.addElement("DateStart");
        vCol.addElement("DateEnd");

        DefaultTableModel tblModel = new DefaultTableModel(vRow, vCol);
        tbFeeLevel.setModel(tblModel);
    }

    public void loadAllFeeManagement() {
        lst.clear();
        lst = feebuss.readByAll();
        loadTableFeeLevel(lst);
    }

    private void loadTableFeeDetail(int index) {

        ArrayList<Fee> list = feebuss.readByAll();
        for (int i = 0; i < list.size(); i++) {
            if (index != list.get(i).getFeeLevelId()) {
                list.remove(i);
                i--;
            }
        }
        loadAllFeeManagement();
    }


    private void btnAddFeeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddFeeActionPerformed
        // TODO add your handling code here:
        if (txtFeeLevelId.getText().trim().equals("")
                || txtPaidTime.getText().trim().equals("")
                || txtTotal.getText().trim().equals("")
                || dcBegin.getDate().equals(null)
                || dcEnd.getDate().equals(null)
                ){
            JOptionPane.showMessageDialog(this, "Please fill in all the blanks!");
            return;
        }
        confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "New FeeLevel", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
        if (confirm == JOptionPane.YES_OPTION) {
            fee.setFeeLevelId(Integer.parseInt(txtFeeLevelId.getText()));
            fee.setPaidTime(Integer.parseInt(txtPaidTime.getText()));
            fee.setMoneyTotal(Float.parseFloat(txtTotal.getText()));
            fee.setDateStart(new java.sql.Date(dcBegin.getDate().getTime()));
            fee.setDateEnd(new java.sql.Date(dcEnd.getDate().getTime()));


            if (feebuss.addfee(fee)) {
                JOptionPane.showMessageDialog(this, feebuss.getLastError());
                txtFeeLevelId.setText(null);
                txtPaidTime.setText(null);
                txtTotal.setText(null);
                dcBegin.setDate(null);
                dcEnd.setDate(null);

                txtFeeLevelId.requestFocus();
                vRow.clear();
                loadAllFeeManagement();
                this.tbFeeLevel.updateUI();
            } else {
                JOptionPane.showMessageDialog(this,feebuss.getLastError());
            }
        } else {

            txtFeeLevelId.setText(null);
            txtPaidTime.setText(null);
            txtTotal.setText(null);
            dcBegin.setDate(null);
            dcEnd.setDate(null);
            txtFeeLevelId.requestFocus();
        }

    }//GEN-LAST:event_btnAddFeeActionPerformed

    private void btnEditFeeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditFeeActionPerformed
        // TODO add your handling code here:
        int selectedIndex = tbFeeLevel.getSelectedRow();
        Fee feelevel = lst.get(selectedIndex);
        int feeid = feelevel.getFeeLevelId();
        if (txtFeeLevelId.getText().trim().equals("")
                || txtPaidTime.getText().trim().equals("")
                || txtTotal.getText().trim().equals("")
                || dcBegin.getDate()==null
                || dcEnd.getDate()==null
                ) {
            JOptionPane.showMessageDialog(this, "Please fill in all the blanks!");
            return;
        }

        confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "Update FeeLevel", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);

        if (confirm == JOptionPane.YES_OPTION) {

            fee.setFeeLevelId(Integer.parseInt(txtFeeLevelId.getText()));
            fee.setPaidTime(Integer.parseInt(txtPaidTime.getText()));
            fee.setMoneyTotal(Float.parseFloat(txtTotal.getText()));
            fee.setDateStart(new java.sql.Date(dcBegin.getDate().getTime()));
            fee.setDateEnd(new java.sql.Date(dcEnd.getDate().getTime()));

            if (feebuss.update(fee)) {
                JOptionPane.showMessageDialog(this, fee.getLastError());
                vRow.clear();
                loadAllFeeManagement();
                this.tbFeeLevel.updateUI();
                txtFeeLevelId.setText(null);
                txtPaidTime.setText(null);
                txtTotal.setText(null);
                dcBegin.setDate(null);
                dcEnd.setDate(null);
            } else {
                JOptionPane.showMessageDialog(this, feebuss.getLastError());
            }
        }
    }//GEN-LAST:event_btnEditFeeActionPerformed

    private void btnDeleteFeeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteFeeActionPerformed
        // TODO add your handling code here:
        int selectedIndex = tbFeeLevel.getSelectedRow();
        Fee feelevel = lst.get(selectedIndex);
        int feeId = feelevel.getFeeLevelId();

        confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "Delete Employee", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
        
        if (confirm == JOptionPane.YES_OPTION) {
            if (feebuss.delete(feeId)) {
                JOptionPane.showMessageDialog(this, feebuss.getLastError());
                txtFeeLevelId.setText(null);
                txtPaidTime.setText(null);
                txtTotal.setText(null);
                dcBegin.setDate(null);
                dcEnd.setDate(null);
                vRow.clear();
                loadAllFeeManagement();
                this.tbFeeLevel.updateUI();
            } else {
                JOptionPane.showMessageDialog(this, feebuss.getLastError());
            }
        }
    }//GEN-LAST:event_btnDeleteFeeActionPerformed

    private void tbFeeLevelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbFeeLevelMouseClicked
        // TODO add your handling code here:
        int selectedIndex = tbFeeLevel.getSelectedRow();
        if (selectedIndex > -1) {
            // RateEntity rate = lst.get(selectedIndex);
            txtFeeLevelId.setText(String.valueOf(lst.get(selectedIndex).getFeeLevelId()));
            txtPaidTime.setText(String.valueOf(lst.get(selectedIndex).getPaidTime()));
            txtTotal.setText(String.valueOf(lst.get(selectedIndex).getMoneyTotal()));
//            SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
            dcBegin.setDate(lst.get(selectedIndex).getDateStart());
            dcEnd.setDate(lst.get(selectedIndex).getDateEnd());

        }
    }//GEN-LAST:event_tbFeeLevelMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddFee;
    private javax.swing.JButton btnDeleteFee;
    private javax.swing.JButton btnEditFee;
    private com.toedter.calendar.JDateChooser dcBegin;
    private com.toedter.calendar.JDateChooser dcEnd;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbFeeLevel;
    private javax.swing.JTextField txtFeeLevelId;
    private javax.swing.JTextField txtPaidTime;
    private javax.swing.JTextField txtTotal;
    // End of variables declaration//GEN-END:variables

   
}
